package com.cxs.distributeddemo.provider;

import com.cxs.distributeddemo.ResourceTable;
import com.cxs.distributeddemo.been.Contacter;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class ContactProvider extends BaseItemProvider {
    private List<Contacter> contactArray;
    private Context context;
    private AdapterClickListener adapterClickListener;

    public ContactProvider(Context context, List<Contacter> contactArray) {
        this.contactArray = contactArray;
        this.context = context;
    }

    @Override
    public int getCount() {
        return contactArray == null ? 0 : contactArray.size();
    }

    @Override
    public Object getItem(int i) {
        if (i >= 0 && i < contactArray.size()) {
            return contactArray.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     *
     * @param i
     * @param component 可复用的组件, 值为空时代表还没有生成item对象
     * @param componentContainer
     * @return
     */
    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        // 可复用的组件: item_contact.xml对象
        Component cpt = component;
        if (cpt == null) {
            /*
                可复用的组件为空时，代表还没有生成item，viewHolder也没有创建
                需要用LayoutScatter生成item对象
             */
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_contact, null, false);
            viewHolder = new ViewHolder();
            Component cptName = cpt.findComponentById(ResourceTable.Id_name);
            if (cptName instanceof Text) {
                viewHolder.name = (Text)cptName;
            }
            Component cptPhone = cpt.findComponentById(ResourceTable.Id_phone);
            if (cptPhone instanceof Text) {
                viewHolder.phone = (Text)cptPhone;
            }
            viewHolder.edit = (Button)cpt.findComponentById(ResourceTable.Id_edit);
            viewHolder.delete = (Button)cpt.findComponentById(ResourceTable.Id_delete);
            cpt.setTag(viewHolder);
        } else {
            if (cpt.getTag() instanceof ViewHolder) {
                viewHolder = (ViewHolder)cpt.getTag();
            }
        }
        if (viewHolder != null) {
            /*
                设置每一个item项的值和事件
             */
            viewHolder.name.setText(contactArray.get(i).getName());
            viewHolder.phone.setText(contactArray.get(i).getPhone());
            viewHolder.edit.setClickedListener(c -> {
                if (adapterClickListener != null) {
                    adapterClickListener.edit(i);
                }
            });
            viewHolder.delete.setClickedListener(c -> {
                if (adapterClickListener != null) {
                    adapterClickListener.delete(i);
                }
            });
        }

        return cpt;
    }

    public interface AdapterClickListener {
        void edit(int position);

        void delete(int position);
    }

    public void setAdapterClickListener(AdapterClickListener adapterClickListener) {
        this.adapterClickListener = adapterClickListener;
    }

    // 需要复用的组件
    private static class ViewHolder {
        private Text name;
        private Text phone;
        private Button edit;
        private Button delete;
    }
}
